package com.aho.app.yourfancy;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class DrawObject {

	private Bitmap mBitmap;
	private Rect mRect;
	
	public DrawObject() {
		mRect = new Rect();
	}
	
	public void init(Bitmap bitmap, int cx, int cy, int w, int h) {
		mBitmap = bitmap;
		mRect.set((int)(cx - w / 2.0f), (int)(cy - h / 2.0f), (int)(cx + w / 2.0f), (int)(cy + h / 2.0f));
	}

	public void draw(Canvas canvas) {
		canvas.drawBitmap(mBitmap, null, mRect, null);
	}
	
	public void setBitmap(Bitmap bitmap) {
		mBitmap = bitmap;
	}
	
	public void setSize(int w, int h) {
		final int cx = mRect.centerX();
		final int cy = mRect.centerY();
		mRect.set((int)(cx - w / 2.0f), (int)(cy - h / 2.0f), (int)(cx + w / 2.0f), (int)(cy + h / 2.0f));
	}
	
	public void postScale(float ds) {
		final int cx = mRect.centerX();
		final int cy = mRect.centerY();
		final float w = (float)mRect.width() * ds;
		final float h = (float)mRect.height() * ds;
		mRect.set((cx - Math.round(w / 2.0f)), cy - Math.round(h / 2.0f), cx + Math.round(w / 2.0f), cy + Math.round(h / 2.0f));
	}
	
	public void setCenterXY(int cx, int cy) {
		final int w = mRect.width();
		final int h = mRect.height();
		mRect.set((int)(cx - w / 2.0f), (int)(cy - h / 2.0f), (int)(cx + w / 2.0f), (int)(cy + h / 2.0f));
	}
	
	public void postCenterXY(int dx, int dy) {
		final int cx = mRect.centerX() + dx;
		final int cy = mRect.centerY() + dy;
		final int w = mRect.width();
		final int h = mRect.height();
		mRect.set((int)(cx - w / 2.0f), (int)(cy - h / 2.0f), (int)(cx + w / 2.0f), (int)(cy + h / 2.0f));
	}
	
	public boolean hitTestRect(int x, int y) {
		return (x < mRect.right && x > mRect.left && y > mRect.top && y < mRect.bottom) ? true : false;
	}

	public boolean hitTestSqure(int x, int y){
		final int cx = mRect.centerX();
		final int cy = mRect.centerY();
		float radius = Math.abs((mRect.top - mRect.bottom) / 2.0f);
		return (((cx - x) * (cx - x) + (cy - y) * (cy - y)) < radius * radius) ? true : false;
	}
}
